home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / Demos / Grating.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-07  |  4.7 KB  |  132 lines  |  [TEXT/KAHL]

  1. /*
  2. Grating.c
  3. This demo shows how to load the clut and put a vignetted grating onto the
  4. screen. This demo has been kept as simple as possible, to enhance readability,
  5. forsaking accurate control over time and contrast of the stimulus. The
  6. FlickeringGrating demo, on the other hand, is somewhat more elaborate and
  7. presents a research-grade stimulus if the LuminanceRecord.h calibration data are
  8. accurate.
  9.  
  10. For a real experiment there are some refinements you should consider. This demo
  11. takes a while to create the image on the screen, but you probably want
  12. frame-accurate timing in your experiment. You could either create the image in
  13. an offscreen GWorld and copy it to the screen with CopyBitsQuickly. Or you could
  14. set the clut to uniform gray (every clut entry equal to the background
  15. luminance) while you're computing the image on-screen, and then load a grayscale
  16. ramp into the clut when you want the display to start. Similarly you can make
  17. the image disappear by either loading a new image, clearing the screen by
  18. calling EraseRect(), or loading a uniform-gray clut.
  19.  
  20. Another issue that matters for a real experiment is gamma correction. The
  21. numbers loaded into the clut are faithfully transformed into voltages by the
  22. three digital-to-analog converters on your video card, but the resulting
  23. luminance produced on your monitor will be an approximately parabolic function
  24. of the video voltage. Apple provides crude gamma correction by means of a
  25. generic 8-bit gamma table in the video driver, which does make things look
  26. better, but is not accurately matched to the gamma of your particular monitor,
  27. which depends on the current settings of its brightness and contrast knobs.
  28. Furthermore the Apple 8-bit gamma-correction scheme, which simply transforms the
  29. nominal 8-bit clut value into a new 8-bit clut value, necessarily restricts the
  30. range of available values, mapping several onto one output value, and omitting
  31. some output values. In other words your luminances will be 8-bit quantized
  32. twice, both before and after gamma correction. It is preferable to do gamma
  33. correction first, without quantization. Therefore I suggest you eliminate
  34. Apple's gamma correction, by calling GDUncorrectedGamma(), and use the
  35. Luminance.c package to do gamma correction. That package implements the
  36. published algorithm of Pelli and Zhang (1991). The FlickeringGrating demo uses
  37. Luminance.c.
  38.  
  39. HISTORY:
  40. 2/7/93    dgp wrote it, as an answer to questions from Bill Merigan and David Brainard.
  41. 2/18/93    dgp    added fpu test.
  42. 2/23/93    dgp    use new GDOpenWindow1 and GDDisposeWindow1.
  43. */
  44. #include "VideoToolbox.h"
  45. #include <math.h>
  46. #if THINK_C
  47.     #include <console.h>
  48. #endif
  49. #define SIZE 300
  50.  
  51. void main(void)
  52. {
  53.     int i,j,error,colors;
  54.     GDHandle device;
  55.     static ColorSpec table[256];
  56.     WindowPtr window,oldPort,w;
  57.     Rect r;
  58.     double a,fX[SIZE],fY[SIZE];
  59.     char string[100];
  60.     Point pt={100,0};
  61.     
  62.     StackGrow(10000);
  63.     #if THINK_C
  64.         console_options.top = 0;
  65.         console_options.left = 0;
  66.         console_options.nrows = 4;
  67.         console_options.ncols = 60;
  68.         printf("\n");
  69.     #else
  70.         InitGraf((Ptr) &thePort);
  71.         InitFonts();
  72.         InitWindows();
  73.         InitCursor();
  74.     #endif
  75.     printf("Welcome to Grating.\n");
  76.     Require(gestalt8BitQD);
  77.     GetPort(&oldPort);
  78.     device=GetScreenDevice(0);
  79.     switch((*device)->gdType){
  80.     case fixedType:
  81.         printf("Alas, this device has a fixed clut. Proceeding anyway.\n");
  82.         break;
  83.     case clutType:
  84.         break;
  85.     case directType:
  86.         printf("Excuse me while I change the pixel depth.\n");
  87.         error = SetDepth(device,eightBitMode,0,0);
  88.         if(error)error = SetDepth(device,fourBitMode,0,0);
  89.         if(error)error = SetDepth(device,twoBitMode,0,0);
  90.         if(error)error = SetDepth(device,oneBitMode,0,0);
  91.         if(error)printf("Alas, this device doesn't support less than 16-bit pixels, but we'll proceed anyway.\n");
  92.         break;
  93.     }
  94.     colors=(**(**(**device).gdPMap).pmTable).ctSize+1;
  95.     // Load the clut with a grayscale ramp.
  96.     for(i=0;i<colors;i++){
  97.         table[i].rgb.red=table[i].rgb.green=table[i].rgb.blue=i*(long)0xffff/(colors-1);
  98.     }
  99.     switch((*device)->gdType){
  100.     case fixedType:
  101.         break;
  102.     case clutType:
  103.         error=GDSetEntries(device,0,colors-1,table);
  104.         break;
  105.     case directType:
  106.         error=GDDirectSetEntries(device,0,colors-1,table);
  107.         break;
  108.     }
  109.     SetMouse(pt);
  110.     window=GDOpenWindow1(device);
  111.     SetPort(window);
  112.     PmBackColor((colors-1)*0.5);    // This works because GDOpenWindow1 marked
  113.     EraseRect(&window->portRect);    // all the colors as pmExplicit.
  114.     SetRect(&r,0,0,SIZE,SIZE);
  115.     CenterRectInRect(&r,&window->portRect);
  116.     for(i=0;i<SIZE;i++){
  117.         a=(i-SIZE/2)/(SIZE/6.);
  118.         fY[i]=exp(-a*a);
  119.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  120.     }
  121.     for(j=0;j<SIZE;j++){
  122.         unsigned long row[SIZE];
  123.         for(i=0;i<SIZE;i++) row[i]=0.5+(colors-1)*0.5*(1.0+fY[j]*fX[i]);
  124.         SetPixelsQuickly(r.left,j+r.top,row,SIZE);
  125.     }
  126.     printf("Done. Hit return to quit.\n");
  127.     gets(string);
  128.     SetPort((WindowPtr)oldPort);
  129.     GDDisposeWindow1(window);
  130.     abort();
  131. }
  132.